<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="b" uri="/b2b-tags"%>
<div class="pro_list fr">
	<div class="title">
		<span class="titletxt">您现在的位置：</span>
		<ul>
			<li class="root">会员中心</li>
			<li>其他管理</li>
			<li>资料下载</li>
		</ul>
	</div>
	<c:if test="${not empty ret }">
		<c:if test="${ret.code ne '200'}">
			<script type="text/javascript">
				alertError('${ret.result}');
			</script>
			<div id=messager class="messager">
				<div class="contentmsg">
					<h5>提示信息～！</h5>
					<ul>
						<li>${ret.result}</li>
					</ul>
				</div>
				<div class="closer" onClick="showHideLayer('messager');" title="关闭"></div>
			</div>
		</c:if>
	</c:if>
	<br />
	<div class="easyui-panel" title="资料下载" style="">
		<div data-options="region:'north',title:'',split:true"
			style="height: 100px; background-color: #e4e4e4;">
			<form action="" id="searchForm" name="searchForm">
				<table>
					<tr>
						<td class="screen-n" style="height: 30px;>
							<span>质检单下载：&nbsp;</span>
						</td>
						<td class="screen-n">
							<input type="text" id="djbh" minlength="8" class="easyui-input"><span style="color:#FF0000">(至少输入8位)&nbsp;</span>
						</td>
						<td class="screen-n">
							<a class="easyui-linkbutton" onclick="initProducts()">查询</a>&nbsp;
						</td>
						<td class="screen-n">
							<a class="easyui-linkbutton" onclick="uploadOrderQuality()">打包下载全部资料</a>&nbsp;
						</td>
						<td class="screen-n">
							<a class="easyui-linkbutton"onclick="pms.resetForm('searchForm')">重置</a>&nbsp;
						</td>
					</tr>
					<tr>
						<td class="screen-n" style="height: 30px;>
							<span>首营资料下载：&nbsp;</span>
						</td>
						<td class="screen-n">
							<input type="text" id="djbh" minlength="8" class="easyui-input"><span style="color:#FF0000">(至少输入8位)&nbsp;</span>
						</td>
						<td class="screen-n">
							<a class="easyui-linkbutton" onclick="initProducts()">查询</a>&nbsp;
						</td>
						<td class="screen-n">
							<a class="easyui-linkbutton" onclick="uploadOrderQuality()">打包下载全部资料</a>&nbsp;
						</td>
						<td class="screen-n">
							<a class="easyui-linkbutton"onclick="pms.resetForm('searchForm')">重置</a>&nbsp;
						</td>
					</tr>
				</table>
			</form>
		</div>

		<div data-options="region:'center',split:true"
			style="width: 100%; height: 385px">
			<table id="tableGrid"></table>
		</div>
	</div>
</div>

<div id="dlg-buttons" style="display: none;">
	<table cellpadding="0" cellspacing="0" style="width: 90%">
		<tr>
			<td style="text-align: center;">
			<a href="#"class="easyui-linkbutton" iconcls="icon-save" onclick="javascript:savecompact()">同意</a>
			<a href="#"class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#pay_compact_win').dialog('close')">拒绝</a>
			</td>
		</tr>
	</table>
</div>
<script>
	$(function() {
		initProducts();
	});
	
	function initProducts(){
		var length = $("#djbh").val().length;
		if(length>=8 || length == 0){
			productsTable = $('#tableGrid').pDataGrid({
				 columns:[[
				          	{field:'djbh',title:'单据号',align:'center',sortable:true},
				          	{field:'spbh',title:'商品编号',align:'center',sortable:true},
							{field:'spmch',title:'商品名称',align:'center',sortable:true},
				          	{field:'shpgg',title:'商品规格',align:'center',sortable:true},
				          	{field:'pihao2',title:'批号',align:'center',sortable:true},
				          	{field:'shengccj',title:'生产厂家',align:'center',sortable:true},
				          	{field:'leibie',title:'类别',align:'center',sortable:true},
				            {field:'action',title:'操作',align:'center',sortable:true,formatter:function(value,row){
					              var str = '';
									 str+='<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="downErpFile(\''+row.folderpath+'\',\''+row.imageid+'\',\''+row.extend+'\',\''+row.djbh+'\',\''+row.spmch+'\')">下载</a>&nbsp;';
								  return str;
					           }}
				          ]],
				    url:'<c:url value="/xcB2B/getDataPackageERPList.s"/>',  
				    pagination:false,
				    pageSize:30,
				    queryParams:{
				    	 "orderCode":$("#djbh").val()
				    },
					completeCallback:function(data){
						if(data.code == "100"){
							alertError(data.result);
						}else{
							 
						}
					}
			}); 
		}else{
			layer.alert("至少输入8位");
		}
	}
	
	/* 批量打包下载 */
	function uploadOrderQuality() {
		if($("#djbh").val().trim() == "" || $("#djbh").val().length<8){
			layer.alert("请输入单据编号,至少输入8位...");
		}else{
			var queryParam = {
				"orderCode" : $("#djbh").val()
			};
		 	loadExcelCallBack('<c:url value="/xcB2B/uploadOrderQuality.s"/>',
					queryParam); 
		}
	}
	/* 批量打包下载 */
	var canSub = true;
	function loadExcelCallBack(dourl, queryParam) {
		if(canSub){
			$.ajax({
				url : dourl,
				pagination : false,
				type : "POST",
				dataType : "json",
				data : queryParam,
				beforeSend:function(){
					canSub = false;
					layer.alert("资料正在打包整理中,耗时较长,请耐心等待...", {icon: 6});
				},
				complete:function(){
					canSub = true;
				},
				success : function(data) {
					if (data.code == "100") {
						layer.msg("装载要下载的数据出错啦,错误代码：" + data.result);
					} else if (data.code == "101") {
						layer.msg(data.result);
					} else {
						//parent.window.open('<c:url value="/util/load.do?"/>fileName='+data.data.outPath+'&title='+data.data.title);
						location.href = '<c:url value="/util/load.s?"/>fileName='
								+ data.data.outPath + '&title=' + data.data.title;
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					layer.msg(" --下载出错啦 请联系管理员!");
				}
			});
		}else{
			layer.alert("资料正在打包整理中,请耐心等待...");
		}
	}
	
	/*根据路径下载单个文件folderpath 路径, imageName 文件名,extend 文件类型, djbh 单据编号, spmch 产品名称 */
	function downErpFile(folderpath,imageid,extend,djbh, spmch){
		$.ajax({
			url : '<c:url value="/xcB2B/downErpFile.s"/>',
			pagination : false,
			type : "POST",
			dataType : "json",
			data : {"folderpath":folderpath,
					"imageid":imageid,
					"extend":extend,
					"djbh":djbh,
					"spmch":spmch},
			success : function(data) {
				if (data.code == "100") {
					layer.msg("装载要下载的数据出错啦,错误代码：" + data.result);
				} else if (data.code == "101") {
					layer.msg(data.result);
				} else {
					location.href = '<c:url value="/util/load.s?"/>fileName='
							+ data.data.outPath + '&title=' + data.data.title;
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				layer.msg(" --下载出错啦 请联系管理员!");
			}
		});
	}
</script>